home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 48 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: ix.netcom.com!netnews
  2. From: dannyyoo@ix.netcom.com (Danny Yoo)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Arrays-What are they good for?
  5. Date: Mon, 01 Jan 1996 06:00:52 GMT
  6. Organization: Netcom
  7. Message-ID: <30e77573.6571977@nntp.ix.netcom.com>
  8. References: <4c6fc5$jv3$1@mhadf.production.compuserve.com>
  9. NNTP-Posting-Host: ix-scr-ca1-16.ix.netcom.com
  10. X-NETCOM-Date: Sun Dec 31 10:08:56 PM PST 1995
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. Merlin Chowkwanyun <71702.1123@CompuServe.COM> wrote:
  14.  
  15. >
  16. >Hello all....
  17. >
  18. >What are arrays?  After reading about them countless times I cannot understand
  19. >their purpose nor how to use them.  Can someone give me an example showing how
  20. >they can be used?  Thank you..
  21.  
  22.     Arrays are variables that allow you to store lists or elements
  23. and other useful stuff.  In math, arrays would be analogous to sets, I
  24. believe.  You can use arrays to store a list of integers, chars, or
  25. any data type.  The statement:
  26.  
  27.     int myintegers[10];
  28.  
  29.     creates a set of 10 integers, all under the name "myintegers".
  30. You can access any one of them by using its subscript within the
  31. brackets.  For example, you could access the first element of
  32. myintegers like
  33.  
  34.     myintegers[0] = 1;  Yes, it starts at zero.  To access the
  35. tenth variable within myintegers, you'd use myintegers[9].
  36.  
  37.     You can use arrays in loops, to record a bunch of information.
  38. An illustration would be to record 10 numbers from the user and print
  39. them back out again.  (Ok, it doesn't sound like a practical
  40. application, but it's simple.)  Conventionally, you'd do something
  41. like:
  42.     int a, b, c, d, e, f... etc...  but this takes too long.
  43. (imagine if you have to work with 100 numbers, or names!)  Instead,
  44. you can do this:
  45.  
  46.     int numbers[10], counter; // I'm using counter as my increment
  47.     for (counter=0;counter<10;counter++)
  48.         cin >> numbers[counter];
  49.     for (counter=0;counter<10;counter++)
  50.         cout << numbers[counter];
  51.  
  52.     Hope this helps!
  53.